#include "mainWindow.hpp"
#include <iostream>

mainWindow::mainWindow(QWidget *parent, QString yn)
	: QWidget(parent)
{
	fd = -2;
        resize(238, 354);
	setWindowTitle(QApplication::translate("mainWindow", 
				"Armessenger", 0, QApplication::UnicodeUTF8));
	hostNick = yn;
	uName = new QLabel(yn);
	uName->setTextFormat(Qt::AutoText);
	uName->setAlignment(Qt::AlignCenter);

	addUser = new QPushButton(tr("add friend"));
	contacts = new QListWidget;

	connect(addUser, SIGNAL(clicked()), this, SLOT(addContact()));

	layout =  new QVBoxLayout;
	layout->addWidget(uName);
	layout->addWidget(contacts);
	layout->addWidget(addUser);
	setLayout(layout);

	connect(this->contacts, SIGNAL(itemDoubleClicked(QListWidgetItem*)), 
			this, SLOT(chattingWindow(QListWidgetItem*)));
	connect(this, SIGNAL(incomingConnection()), SLOT(addToList()));
}

void mainWindow::eConnections()
{
	emit incomingConnection();
}

void mainWindow::chattingWindow(QListWidgetItem *item)
{
	contact *cnt = (contact*)item;
	connect(cnt->sendMess, SIGNAL(returnPressed()),cnt,SLOT(insertMess()));
	cnt->chtWin->show();
}

void mainWindow::addToList()
{ 
	contact *tmp;
	if(fd > 0)
	{
		tmp = new contact(h, remoteNick, fd, hostNick);
	       	fd = -2;
	}
	else
	{
		tmp = new contact(((h_name->text()).toStdString()), 
			remoteNick, fd, hostNick);
	}
	contacts->addItem( tmp );

}

void mainWindow::addContact()
{
	QWidget *dialog = new QWidget;
     //   dialog->resize(288, 151);

	QPushButton *ok = new QPushButton(tr("OK"));
	QPushButton *cancel = new QPushButton(tr("Cancel"));

	QLabel *hostName = new QLabel (tr("Host name:"));
//	QLabel *nickName = new QLabel (tr("Nick name:"));

	h_name = new QLineEdit;
//	n_name = new QLineEdit;

	QHBoxLayout *layout1 =  new QHBoxLayout;
	layout1->addWidget(hostName);
	layout1->addWidget(h_name);

//	QHBoxLayout *layout2 =  new QHBoxLayout;
//	layout2->addWidget(nickName);
//	layout2->addWidget(n_name);

	QHBoxLayout *layout3 =  new QHBoxLayout;
	layout3->addWidget(ok);
	layout3->addWidget(cancel);

	QVBoxLayout *mainLayout =  new QVBoxLayout;
	mainLayout->addLayout(layout1);
//	mainLayout->addLayout(layout2);
	mainLayout->addLayout(layout3);

	connect(cancel,SIGNAL(clicked()),dialog,SLOT(close()));
	connect(ok,SIGNAL(clicked()),this,SLOT(addToList()));
	connect(ok,SIGNAL(clicked()),dialog,SLOT(close()));
	connect(h_name, SIGNAL(returnPressed()),ok,SIGNAL(clicked()));

	dialog->setLayout(mainLayout);
	dialog->show();
}
